/**
 * 
 */
package com.cap.bts.crmdp.webservice;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;
import com.cap.bts.crmdp.dealer.mars.domain.DataConversionLog;
import com.cap.bts.crmdp.dealer.mars.service.IDataConversionLogService;
import com.cap.bts.crmdp.webservice.dc.DCProperties;
import com.cap.bts.crmdp.webservice.dc.DataConversionQueue;
import com.cap.bts.crmdp.webservice.dto.DtCrmDto;
import com.cap.bts.crmdp.webservice.dto.WSResultDto;
import com.cap.bts.framework.codemapping.service.ICodeMapping;
import com.cap.bts.framework.common.exception.AppException;
import com.cap.bts.framework.common.util.ApplicationContextUtil;


/**
 * @author annson
 *
 */
public class DTCrmFacade {

	private static final Logger LOGGER = Logger.getLogger(DTCrmFacade.class);

	/**
	 * 接收DC Tool 通知
	 * 
	 * @Date
	 * @param dto
	 * @return
	 */
	public WSResultDto process(String token, DtCrmDto dto) {
		WSResultDto resultDto = new WSResultDto();
				
		resultDto.setResultCode("1");
		
		StringBuilder message = new StringBuilder();
		message.append("token:").append(token);
		message.append("\nDealerNumber:").append(dto.getDealerNumber());
		message.append("\nbatchId:").append(dto.getBatchId());
		message.append("\ncustVehicleFile:").append(dto.getCustVehicleFile());
		message.append("\ncustVehicleCount:").append(dto.getCustVehicleCount());
		message.append("\nportfolioFile:").append(dto.getPortfolioFile());
		message.append("\nportfolioCount:").append(dto.getPortfolioCount());
		message.append("\ndescp:").append(dto.getDescp());
		
		LOGGER.info(message.toString());
		
		
		try {
			ICodeMapping codeMappingService = (ICodeMapping) ApplicationContextUtil.getBean("codeMappingImpl");
			String dcInfo = codeMappingService.getCodeValueByCode("DataConvertInfo", "DataConvertServerInfo");
			if(StringUtils.isNotBlank(dcInfo)){
				DCProperties dcProperties = JSON.parseObject(dcInfo, DCProperties.class);
				
				if(!StringUtils.equals(token, dcProperties.getToken())){
					resultDto.setResultMsg("unauthorized");
					resultDto.setResultCode("0");
					LOGGER.error("batchId " + dto.getBatchId() + " token error.");
				}else{
					//加入队列，等待处理
					DataConversionQueue.start();
					//入库等待规定时间再处理
					
					IDataConversionLogService dataConversionLogServiceImpl = (IDataConversionLogService) ApplicationContextUtil.getBean("dataConversionLogServiceImpl");
					DataConversionLog log = new DataConversionLog();
					
					log.setParamText(JSON.toJSONString(dto));
					log.setBatchId(dto.getBatchId());
					log.setStatus("1");
					log.setCreateBy(0L);
					log.setCreateDate(new Date());
					
					dataConversionLogServiceImpl.insert(log);
				}
				
			}else{
				LOGGER.error("DataConvertInfo 服务器未配置.");
				resultDto.setResultMsg("请联系管理员服务器信息未配置.");
				resultDto.setResultCode("0");
			}
			
		} catch (AppException e) {
			LOGGER.error("", e);
		}
		
		return resultDto;

	}

}
